package de.uni.muenster.idw2.xml;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import de.uni.muenster.idw2.data.Dongle;

public class DongleParser {
	Dongle dongle;
	ArrayList<Dongle> dongles = new ArrayList<Dongle>();
	ArrayList<String> imageLinks = new ArrayList<String>();

	public DongleParser(InputStream in) {
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();

			DefaultHandler handler = new DefaultHandler() {
				boolean mac = false;
				boolean latitude = false;
				boolean longitude = false;
				boolean floor = false;
				boolean imageFloor0 = false;
				boolean imageFloor1 = false;
				boolean imageFloor2 = false;

				public void startElement(String uri, String localName,
						String qName, Attributes attributes)
						throws SAXException {
					if (qName.equals("dongle")) {
						dongle = new Dongle();
						imageLinks = new ArrayList<String>();
					}
					if (qName.equals("mac")) {
						mac = true;
					}
					if (qName.equals("latitude")) {
						latitude = true;
					}
					if (qName.equals("longitude")) {
						longitude = true;
					}
					if (qName.equals("floor")) {
						floor = true;
					}
					if (qName.equals("image_floor_0")) {
						imageFloor0 = true;
					}
					if (qName.equals("image_floor_1")) {
						imageFloor1 = true;
					}
					if (qName.equals("image_floor_2")) {
						imageFloor2 = true;
					}
				}

				public void endElement(String uri, String localName,
						String qName) throws SAXException {
					if (qName.equals("dongle")) {
						dongle.setImageLinks(imageLinks);
						dongles.add(dongle);
					}
				}

				public void characters(char ch[], int start, int length)
						throws SAXException {
					if (mac) {
						dongle.setMac(new String(ch, start, length));
						mac = false;
					}
					if (latitude) {
						dongle.setLatitude(Double.parseDouble(new String(ch,
								start, length)));
						latitude = false;
					}
					if (longitude) {
						dongle.setLongitude(Double.parseDouble(new String(ch,
								start, length)));
						longitude = false;
					}
					if (floor) {
						dongle.setFloor(Integer.parseInt(new String(ch, start, length)));
						floor = false;
					}
					if (imageFloor0) {
						String tmp = new String(ch, start, length);
						if (tmp.trim().equals(""))
							imageLinks.add(null);
						else
							imageLinks.add(tmp);
						imageFloor0 = false;
					}
					if (imageFloor1) {
						String tmp = new String(ch, start, length);
						if (tmp.trim().equals(""))
							imageLinks.add(null);
						else
							imageLinks.add(tmp);
						imageFloor1 = false;
					}
					if (imageFloor2) {
						String tmp = new String(ch, start, length);
						if (tmp.trim().equals(""))
							imageLinks.add(null);
						else
							imageLinks.add(tmp);
						imageFloor2 = false;
					}
				}
			};
			saxParser.parse(in, handler);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ArrayList<Dongle> getDongles() {
		return dongles;
	}
}